﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;

using yncTech.Common;
using yncTech.BusinessObjects;
using EntitySpaces.Interfaces;


public partial class Admin_Fin_Fin003b : AdminBasePage
{
    decimal shipID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }
        if (!IsPostBack)
        {
            FinShipping fs = new yncTech.BusinessObjects.FinShipping();
            fs.LoadByPrimaryKey(shipID);

            drpLocation.SelectedValue = fs.FromDestination.ToString();
        }

    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    /// <summary>
    /// 只加载状态为"存储中",location 跟shipping from location一致,box状态为close而且不是转移中的box
    /// </summary>
    /// <param name="page"></param>
    void GridDataBind(int page)
    {
        VBoxReceiptsCollection boxes = new VBoxReceiptsCollection();
        boxes.Query.Where(boxes.Query.BoxLocation == drpLocation.SelectedValue && 
            boxes.Query.InTransfer == 0 && 
            boxes.Query.StorageStatus == ConstantValue.StorageStatusCode.InStock && 
            boxes.Query.ReceiptCount > 0 );

        if (txtBoxNo.Text.Trim().Length > 0)
        {
            boxes.Query.Where(boxes.Query.BoxNo.Like(string.Format("%{0}%",txtBoxNo.Text)));
        }

        pageGrid.DataSource = boxes.Query.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GridDataBind(1);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
            FinShippingBoxes fsb = new yncTech.BusinessObjects.FinShippingBoxes();
            FinBox finBox = new yncTech.BusinessObjects.FinBox();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbBox = (Label)item.FindControl("lbBoxID");
                        fsb.BoxID = decimal.Parse(lbBox.Text);
                        fsb.ShipID = shipID;
                        fsb.UpdatedBy = fsb.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        fsb.UpdatedDttm = fsb.CreatedDttm = DateTime.Now;
                        fsb.Save();

                        //添加后将box的状态设置为:待转移
                        finBox.LoadByPrimaryKey(fsb.BoxID.Value);
                        finBox.StorageStatus = ConstantValue.StorageStatusCode.PendingTransfer;
                        finBox.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        finBox.UpdatedDttm = DateTime.Now;
                        finBox.Save();
                        //将盒中的凭证也设置为:待转移

                        FinBoxReceiptsCollection boxRecs = new yncTech.BusinessObjects.FinBoxReceiptsCollection();
                        boxRecs.Query.Where(boxRecs.Query.BoxID == fsb.BoxID.Value);
                        boxRecs.Query.Load();

                        FinReceiptCollection receipts = new yncTech.BusinessObjects.FinReceiptCollection();
                        receipts.Query.Where(receipts.Query.ReceiptID.In(boxRecs.Select(r=>r.ReceiptID.Value).ToArray()));
                        receipts.Query.Load();
                        foreach (var rec in receipts)
                        {
                            rec.StorageStatus = ConstantValue.StorageStatusCode.PendingTransfer;
                            rec.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                            rec.UpdatedDttm = DateTime.Now;
                        }
                        receipts.Save();


                    }
                }
                tr.Complete();

                WebHelper.CloseDialog(true);

            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}